<style>
.layui-table-cell {
	height: auto !important;
	white-space: normal;
}
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-card-body table-tool-mini">
			<div class="layui-form toolbar" id="tbToolBar">
				<div class="layui-form-item">
					<div class="layui-inline">
						<button id="btnAddBizMap" class="layui-btn icon-btn">
							<i class="layui-icon">&#xe654;</i>新增
						</button>
					</div>
					<div class="layui-inline" style="float: right; margin-right: 0px;">
						<div class="layui-inline"
							style="padding-right: 0px; margin-right: 0px;">
							<label class="layui-form-label w-auto">名称</label>
							<div class="layui-input-inline mr0">
								<input name="name" class="layui-input" type="text"
									placeholder="请输入名称" />
							</div>
						</div>
						<div class="layui-inline"
							style="padding-right: 0px; margin-right: 0px;">
							<select name="isPublic"
								style="width: 110px; height: 38px; border-style: solid; border-radius: 2px; border-color: #e6e6e6;">
								<option value="">--请选择--</option>
								<option value="0">公开</option>
								<option value="1">私有</option>
							</select>
						</div>
						<div class="layui-inline"
							style="padding-right: 0px; margin-right: 0px;">
							<button class="layui-btn icon-btn"
								lay-filter="formSubSearchBizMap" lay-submit>
								<i class="layui-icon">&#xe615;</i>查询
							</button>
						</div>
					</div>
				</div>
				<table class="layui-table" id="tableBizMap" lay-filter="tableBizMap"></table>
			</div>
		</div>
	</div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarBizMap">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateBizMap">
#[[  {{#  if(d.enabled === '1'){ }}
    <span style="color: #23aa2e;">正常</span>
  {{#  } else { }}
	<span style="color: #ff0000;">禁止</span>
  {{#  } }}
]]#
</script>

<script type="text/html" id="tableIsPublicBizMap">
#[[  {{#  if(d.isPublic === '0'){ }}
    <span style="color: #23aa2e;">公开</span>
  {{#  } else { }}
	<span style="color: #ff0000;">私有</span>
  {{#  } }}
]]#
</script>


<!-- 表单弹窗-->
<script type="text/html" id="modelBizMap"> 
    <form id="modelBizMapForm" lay-filter="modelBizMapForm" class="layui-form model-form" style="padding: 5px;">
        <input name="id" type="hidden"  />
		<input name="img" type="hidden" id="bizMap-coverImg" />
		<table class="formTable">
			<tr>
				<td rowspan="4" style="text-align: center;">
			    	<img id="bizMap-cover" src="#(ctx)/assets/img/thum-default.jpg" width="135" height="135">
				</td>
			</tr>
			<tr>
				<td>名称</td>
				<td colspan="2" style="text-align: left;">
                	<input name="name"  placeholder="请输入名称" type="text" class="layui-input" lay-verType="tips" lay-verify="required" required/>
                </td>
			</tr>
			<tr>
				<td>标识</td>
				<td  colspan="2" style="text-align: left;">
                	<input name="code"  placeholder="请输入标识" type="text" class="layui-input" />
                </td>
			</tr>
			<tr>
				<td>坐标系</td>
				<td  colspan="2" style="text-align: left;">
                	<input name="coordinate"  placeholder="请输入坐标系" type="text" class="layui-input" />
                </td>
			</tr>
			<tr>
				<td>服务类型</td>
				<td  colspan=3" style="text-align: left;">
			      <select name="serverType" lay-filter="serverType"  lay-verify="required" required>
			        <option value="">--请选择--</option>
			        <option value="URL" >URL</option>
			        <option value="WMS" >WMS</option>
					<option value="WMTS" >WMTS</option>
			      </select>
                </td>
			</tr>
			<tr>
				<td>数据类型</td>
				<td colspan=3"  style="text-align: left;">
			      <select name="dataType" lay-filter="dataType"  lay-verify="required" required>
			        <option value="">--请选择--</option>
			        <option value="terrain" >地形数据</option>
			        <option value="map" >底图数据</option>
			      </select>
                </td>
			</tr>
			<tr>
				<td>是否启用</td>
				<td colspan=3" style="text-align: left;">
			      <select name="enabled" lay-filter="enabled"  lay-verify="required" required>
			        <option value="">--请选择--</option>
			        <option value="0" >禁用</option>
			        <option value="1" >启用</option>
			      </select>
                </td>
			</tr>
			<tr>
				<td style="text-align: left;">序号</td>
				<td colspan=3">
                	<input name="sort" placeholder="请输入序号" type="number"   class="layui-input"  
                       lay-verType="tips" lay-verify="required|number" required/>
                </td>
			</tr>
			<tr>
				<td colspan="4" style="text-align: left;">图层参数</td>
			</tr>
			<tr>
				<td colspan="4" style="text-align: left;">
					<textarea name="parameters" placeholder="只能识别josn格式数据" class="layui-textarea" style="height: 200px;"></textarea>
                </td>
			</tr>
			<tr>
				<td colspan="4">
		            <button class="layui-btn" lay-filter="modelSubmitBizMapForm" lay-submit>保存</button>
                </td>
			</tr>												
		</table>    
    </form>
</script>

<script type="text/html" id="modelIconForBizMap"> 
    <div class="layui-card" style="background: none;">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar" id="tbToolBar">
                <div class="layui-form-item">
                    <div class="layui-inline" style="float: left;">
						<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
							<div class="layui-input-inline mr0">
								<input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
							</div>
						</div>
						<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
							<button class="layui-btn layui-btn-sm layui-btn-orange icon-btn" lay-filter="formSubSearchIconForBizMap" lay-submit>
								<i class="layui-icon">&#xe615;</i>查询
							</button>
							<button class="layui-btn layui-btn-sm layui-btn-orange icon-btn" id="uploadIconBtn">
								<i class="layui-icon">&#xe67c;</i>上传
							</button>
						</div>					
					</div>
				</div>
				<table class="layui-table layui-img-table" id="tableIconForBizMap" lay-filter="tableIconForBizMap" style="margin: 1px 0;"></table>
			</div>
		</div>
	</div>
</script>


<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'config','colorpicker','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var upload=layui.upload;
        var admin = layui.admin;
        var config = layui.config;
        var colorpicker = layui.colorpicker;
        form.render();
        // 渲染表格
        var insTb = table.render({
            elem: '#tableBizMap',
            url: '#(ctx)/biz/map/listData', 
            page: true,
            toolbar: false,
            cols: [[
                {type: 'numbers', title: '序号'},
                {
                    align: 'center', templet: function (d) {
                    	if(d.img)
                        	return '<img src="#(ctx)/' + d.img + '"  />';
                        else
                        	return '';
                    }, title: '缩略图', width: 90, unresize: true
                }, 
                {field: 'name', sort: false, title: '名称'},
                {field: 'code', sort: false, title: '标识'},
                {field: 'isPublic', sort: false, templet: '#tableIsPublicBizMap', title: '状态', align: 'center'},
                {field: 'status', sort: false, templet: '#tableStateBizMap', title: '状态', align: 'center'},
                {field: 'createUserName', sort: false, title: '作者'},
                {
                    field: 'createTime', sort: false, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'left', toolbar: '#tableBarBizMap', title: '操作'}
            ]],
        });
        // 添加
		$('#btnAddBizMap').click(function () {
			 showEditModel();
		});
        // 搜索
        form.on('submit(formSubSearchBizMap)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });
        
        // 工具条点击事件
        table.on('tool(tableBizMap)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
            	showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.name);
            }
        });

        // 删除
        function doDel(id, name) {
            layer.confirm('确定要删除“【<span style="color:red;">' + name + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('biz/map/delete', {
                    id: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }


        // 显示表单弹窗
        function showEditModel(data) {
            layer.open({
                type: 1,
                shade: 0.3,
                offset: 't'	,
                title: (data ? '修改' : '添加') + '',
                area:'500px',
                resize :false,
                content: $('#modelBizMap').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    form.val('modelBizMapForm', data);  // 回显数据
                    if(data && data.img){
                    	$('#bizMap-cover').attr("src",data.img);
                    }
			                    
					$('#bizMap-cover').click(function () {
						selectIcon(function(e){
							$('#bizMap-cover').attr("src","#(ctx)/"+e.url);
	 			    	 	$('#bizMap-coverImg').val(e.url);
						})
					});                    
                    
    			    form.on('submit(modelSubmitBizMapForm)', function (data) {
    			        layer.load(2);
    			        var formData={}; 
    			        for(var key in data.field){
    			        	if('file'!==key)
    			        		formData['bizMap.'+key]=data.field[key];
    			        }
                        admin.req("biz/map/save",formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'post');
    			        return false;
    			    });
    			    form.render(); 
                },
                end:function(){
                }
            });
        }
        
        function selectIcon(callback){
            layer.open({
                type: 1,
                title:'图标库',
                area:'250px',
                resize :false,
                shade:0,
                offset: 't',	
                fixed:true,
                move:false,
                content: $('#modelIconForBizMap').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var insTbIconForBizMap = table.render({
                        elem: '#tableIconForBizMap',
                        url: '#(ctx)/biz/icon/listData',
                        page: true,
                        toolbar: false,
                        skin:'nob',// 无边框风格
                        page: {
                            layout: ['prev', 'page', 'next','count'] //自定义分页布局
                            ,groups:5 //只显示 1 个连续页码
                            ,first: false //不显示首页
                            ,last: false //不显示尾页
                        },   
                        where:{
                        	type:'mapThumbnail'
                        },
                        cols: [[
                            {
                                align: 'center', templet: function (d) {
                                	if(d.url)
                                    	return '<img src="#(ctx)/'+d.url+'" />';
                                    else
                                    	return '';
                                }, title: '图标',
                            },
                            {field: 'name', sort: false, title: '名称'}
                        ]],
                        done:function () {
                    	}                
                    });
                    
                    upload.render({
                        elem: '#uploadIconBtn'
                        ,url: '#(ctx)/biz/icon/upload?type=mapThumbnail'
                        ,accept: 'images'
                     	,before: function(obj){ 
                  	    layer.load(); 
                  	  }
                  	  ,done: function(res, index, upload){
                  		layer.closeAll('loading'); //关闭loading
                          if (200==res.code) {
                              layer.msg(res.message, {icon: 1});
                              insTbIconForBizMap.reload();
                          } else {
                              layer.msg(res.message, {icon: 2});
                          }
                  	  }
                  	  ,error: function(index, upload){
                  	    	layer.closeAll('loading');
                  	   	 	layer.msg("上传失败！", {icon: 2});
                  	  }       
                    });
                    
                    table.on('row(tableIconForBizMap)', function(obj){
                    	var data = obj.data;
                      	obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                      	if(callback){
                      		callback(data)
                      	}
                    });
                    form.on('submit(formSubSearchIconForBizMap)', function (data) {
                    	insTbIconForBizMap.reload({where: data.field}, 'data');
                    });
                },
                end:function(){
                }
            });
        }
    });
</script>
